from ..common.pipelines import *
from ..common.tasks import *

log = logging.getLogger(__name__)


class CleanRedisTask(Task):
    def __init__(self):
        Task.__init__(self, 'CleanRedisTask')

    def __do_execute__(self, params={}):
        log.info("CleanRedisTask:Entered execute()")
        time.sleep(10)
        log.info("CleanRedisTask:Exit execute()")


class RedisTask(Task):
    def __init__(self):
        Task.__init__(self, 'CleanRedisTask')

    def __do_execute__(self, params={}):
        log.info("CleanRedisTask:Entered execute()")
        time.sleep(10)
        log.info("CleanRedisTask:Exit execute()")


class CleanRedisPipeline(Pipeline):
    def __init__(self, name, **kwargs):
        Pipeline.__init__(self, name, **kwargs)
        self.add_task(CleanRedisTask())


#####################################################################
class RedisSleep10Task(Task):
    def __init__(self):
        Task.__init__(self, 'RedisTaskSleep10')

    def __do_execute__(self, params={}):
        log.info("RedisTaskSleep10:Entered execute()")
        time.sleep(10)
        log.info("RedisTaskSleep10:Exit execute()")


class RedisSleep10Pipeline(Pipeline):
    def __init__(self, name, **kwargs):
        Pipeline.__init__(self, name, **kwargs)
        self.add_task(RedisSleep10Task())


class RedisSleep20Task(Task):
    def __init__(self):
        Task.__init__(self, 'RedisTaskSleep20')

    def __do_execute__(self, params={}):
        log.info("RedisTaskSleep20:Entered execute()")
        time.sleep(20)
        log.info("RedisTaskSleep20:Exit execute()")


class RedisSleep20Pipeline(Pipeline):
    def __init__(self, name, **kwargs):
        Pipeline.__init__(self, name, **kwargs)
        self.add_task(RedisSleep20Task())


class RedisErrorTask(Task):
    def __init__(self):
        Task.__init__(self, 'RedisTaskError')

    def __do_execute__(self, params={}):
        log.info("RedisTaskError:Entered execute()")
        raise TaskExcecuteError("######ERROR##########")
        log.info("RedisTaskError:Exit execute()")


class RedisTestTask(Task):
    def __init__(self):
        Task.__init__(self, 'RedisTestTask')

    def __do_execute__(self, params={}):
        log.info("RedisTestTask:Entered execute()")
        print "redis test..."
        raise TaskExcecuteError()
        log.info("RedisTestTask:Exit execute()")


class RedisTaskErrorPipeline(Pipeline):
    def __init__(self, name, **kwargs):
        Pipeline.__init__(self, name, **kwargs)
        self.add_task(RedisSleep10Task())
        self.add_task(RedisErrorTask())
        self.add_task(RedisTestTask())


class RedisPipelineFactory(PipelineFactory):
    def build_pipelines(self):
        redis_pipelines = []
        for i in xrange(1, 9):
            pipeline_name = "clean_redis" + str(i)
            redis_pipelines.append(
                RedisSleep10Pipeline(pipeline_name, group="redis", param1=123, param2=123))
        pipeline_name = "clean_redis10"
        redis_pipelines.append(
            RedisSleep20Pipeline(pipeline_name, group=pipeline_name, timeout=15, param1=123, param2=123))
        pipeline_name = "clean_redis11"
        redis_pipelines.append(
            RedisTaskErrorPipeline(pipeline_name, group=pipeline_name, timeout=15, param1=123, param2=123))
        return redis_pipelines
